#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
using namespace std;
const int N = 200;
// int id = 0;
int p;
int g[N][N];
int dist[N];
bool st[N];
int ans = -1, len = 0x3f3f3f3f;

// void add(int a, int b, int c) { e[idx] = b, ne[idx] = h[a], h[a] = idx, w[idx++] = c; }

void djstra()
{
    for (int i = 'A'; i < 'z'; ++i)
    {
        // 找到距离最小的位置
        int nd = -1;
        for (int k = 0; k < N; ++k)
            if ((nd == -1 || dist[k] < dist[nd]) && !st[k] && isalpha(k))
                nd = k;
        if (nd == -1) break; // 如果到这里j还是-1，说明更新完了，直接退出即可
        st[nd] = true;
        // 用当前最小的位置去更新其他还没有确定的点
        for (int j = 'A'; j <= 'z'; ++j)
            if (!st[j] && g[nd][j] != 0x3f3f3f3f)
                dist[j] = min(dist[j], dist[nd] + g[nd][j]);
    }
    for (int i = 'A'; i <= 'z'; ++i)
    {
        if (dist[i] < len && i != 'Z' && isupper(i))
            len = dist[i], ans = i;
    }
}

int main()
{
    memset(g, 0x3f, sizeof g);
    memset(dist, 0x3f, sizeof dist);
    cin >> p;
    for (int i = 0; i < p; ++i)
    {
        char a, b;
        int c;
        cin >> a >> b >> c;
        g[a][b] = min(g[a][b], c);
        g[b][a] = min(g[b][a], c);
    }
    dist['Z'] = 0;
    djstra();
    cout << (char)ans << ' ' << len << endl;
    return 0;
}